<?php
/**
 * Created by PhpStorm.
 * User: harlen
 * Date: 2017/2/8 0008
 * Time: 11:52
 */

namespace app\admin\controller;

use app\admin\model\Setting;
use think\Request;

class Site extends BaseController
{
    public $url = 'Site/index';
    public function index()
    {
        $list = Setting::where(['nav'=> 'basic'])->order(['list_order'=> 'desc', 'id'=> 'asc'])->column('*', 'id');

        $info = '';
        foreach($list as $r){
            @extract($r);
            switch($type){
                //普通文本信息
                case 1:
                    $info .= <<<HTML
<div class="form-group ">
    <label class="col-lg-2 control-label" >$remark</label>
    <div class="col-lg-3">
        <input type="text" value="$value" class="form-control" name="$name">
    </div>
</div>
HTML;
                    break;

                //TODO::生成图片表单
                case 2:
                    break;
                //其他文件
                case 3:
                    break;
                default:
                    break;
            }
        }
        $this->assign('info', $info);
        return $this->fetch('', ['list'=>$list]);
    }
    public function save(Request $request)
    {
        if($request->isPost()){
            $post = $request->post();
            $setting = new Setting();
            //批量更新数设置项
            foreach($post as $name => $data){
                $setting->isUpdate(true, ['name'=>$name])->save(['value'=>$data]);
            }
            return ['code'=>1, 'msg'=> lang('save success'), 'url'=> url($this->url)];
        }
        return ['code'=>0, 'msg'=> lang('invalid request')];
    }

    /**
     * 生成input表单
     */
    protected function genInput($data)
    {

    }

}